import React from "react";
import Button from 'antd/lib/button';
import ConfigProvider from 'antd/lib/config-provider';
import zhCN from 'antd/es/locale/zh_CN';
import TabletypeTable from "@/components/workflow/TabletypeTable";
import notification from "@/services/notification";
import Tabletype from "@/services/tabletype";

export default class TabletypeToolbar extends React.Component {
  state = {
    loading: true,
    isnew: true,
    id: 0,
    code: "",
    name: "",
    type:0,
    numauto: false,
    numhand: false,
    numhead: "",
    numdate: "",
    numfoot: 1,
    footlenth: 4,
  };
  componentWillMount() {
     if(this.props.id<1 || this.props.id===undefined ){
       Tabletype.getid({id: 0}).then(data => {
        this.setState({
          id: data[0].id,
          isnew: true,
          loading: false,
        });
      });
     }else{
        this.refreshData(this.props.id,false)
     }
  }
   refreshData(id = 0,isnew) {
     Tabletype.get({id: id}).then(data => {
       if (data.id > 0) {
         this.setState({
           isnew: isnew,
           id: data.id,//
            code: data.code,//
            name: data.name,//
            type: data.type,
            numauto: data.numauto,//
            numhand: data.numhand,//
            numhead: data.numhead,//
            numdate: data.numdate,//
            numfoot: data.numfoot,//
            footlenth: data.footlenth,//
           loading: false,
         });
       } else {
         this.setState({
           loading: false,
           isnew: isnew,
           id: id,
          code: "",//
          name: "",//
          type:0,
          numauto: false,//
          numhand: false,//
          numhead: "",//
          numdate: "",//
          numfoot: 1,//
          footlenth: 4,//
         });
       }
     }).catch(error => {
       this.setState({
         isnew: isnew,
         loading: false,
         id: id,
          code: "",//
          name: "",//
          type:0,
          numauto: false,//
          numhand: false,//
          numhead: "",//
          numdate: "",//
          numfoot: 1,//
          footlenth: 4,//
       });
     });
   }

  ///|| str.match(/^[\s]*$/)
  isEmptyobj = (str) => {
	if(str==='' || str === null || str === 'null' || str === undefined || str === 'undefined'){
		return true;
	}else{
		return false;
	}
}
  handleSave = () => {
    let  name=this.child.state.name;
    if(this.isEmptyobj(name)){
      notification.error("名称不能为空");
      return;
    }
    let  numfoot=this.child.state.numfoot;
    if(this.isEmptyobj(numfoot)){
      notification.error("流水号起始值不能为空");
      return;
    }
    let  footlenth=this.child.state.footlenth;
    if(this.isEmptyobj(numfoot)){
      notification.error("流水号长度不能为空");
      return;
    }
    let data={id: this.child.state.id,//
              code: this.child.state.code,//
              name: name,//
              type: this.state.type,
              numauto:this.child.state.numauto,//
              numhand: this.child.state.numhand,//
              numhead:this.child.state.numhead,//
              numdate: this.child.state.numdate,//
              numfoot: numfoot,//
              footlenth: footlenth}
    if(this.state.isnew) {
      Tabletype.create(data)
        .then(() => {
           this.setState({
                isnew: false,
                loading: false,
              });
          notification.success("单据新增成功.");
        })
        .catch(error => {
          notification.error(error.message, error.detailedMessage);
        });
    }else{
      Tabletype.save(data)
        .then(() => {
          notification.success("单据修改成功.");
        })
        .catch(error => {
          notification.error(error.message, error.detailedMessage);
        });
    }
  };
  childchange = (data) =>{
    this.setState({
        id: data.id,
        code: data.code,//
        name: data.name,//
        numauto: data.numauto,//
        numhand: data.numhand,//
        numhead: data.numhead,//
        numdate: data.numdate,//
        numfoot: data.numfoot,//
        footlenth: data.footlenth,//
    });
  };
  render() {
    if (this.state.loading) return null;
    const data={
        id: this.state.id,
        code: this.state.code,//
        name: this.state.name,//
        numauto:this.state.numauto,//
        numhand: this.state.numhand,//
        numhead:this.state.numhead,//
        numdate: this.state.numdate,//
        numfoot: this.state.numfoot,//
        footlenth: this.state.footlenth,//
    }
    return(
     <ConfigProvider locale={zhCN}>
      <div>
        <div style={{ marginBottom: 16 }}>
          {/*<Button type="primary" onClick={() => this.handleadd()}>*/}
          {/*  <i className="fa fa-plus m-r-5" />*/}
          {/*  新增*/}
          {/*</Button>*/}
          {/*<Divider type="vertical" />*/}
          <Button type="primary" onClick={() => this.handleSave()} >
            <i className="fa fa-save m-r-5" />
            保存
          </Button>
          {/*<Divider type="vertical" />*/}
          {/*<Button type="primary" onClick={() => this.handleDelete()} >*/}
          {/*  <i className="fa fa-trash m-r-5" />*/}
          {/*  删除*/}
          {/*</Button>*/}
        </div>
        <div>
          <TabletypeTable data={data}  isedit={this.state.isedit}  onRef={(ref) => { this.child = ref; }} childchange = {this.childchange}/>
        </div>
      </div>
     </ConfigProvider>
    );
  };
}

